package com.enpronomics.pcall.activity;

//import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.enpronomics.pcall.R;
import com.enpronomics.pcall.ServiceManager;

public class SettingsActivity extends Activity implements OnClickListener {
	private CheckBox activationChB;
	private EditText nameEditText;
	private SeekBar SOSRepetitionSeekBar;
	private SeekBar ActionIntervalSeekBar;
	// private CheckBox privacyStatementChB;
	private TextView SOSRepitionTextView;
	private TextView ActionIntervalTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings_layout);

		activationChB = (CheckBox) findViewById(R.id.settings_panic_checkBox);
		nameEditText = (EditText) findViewById(R.id.name_EditText);
		/*
		 * locationAutoReplaySp=(Spinner)findViewById(R.id.
		 * setings_locationautoreplay_spinner); ArrayAdapter<CharSequence>
		 * adapter = ArrayAdapter.createFromResource( this,
		 * R.array.location_autu_replay, android.R.layout.simple_spinner_item );
		 * adapter.setDropDownViewResource(
		 * android.R.layout.simple_spinner_dropdown_item );
		 * locationAutoReplaySp.setAdapter( adapter );
		 * 
		 * panicContactsBtn=(Button)findViewById(R.id.settings_paniccontacts_button
		 * ); panicContactsBtn.setOnClickListener(this);
		 */

		SOSRepetitionSeekBar = (SeekBar) findViewById(R.id.settings_SOS_repetition_seekBar);
		ActionIntervalSeekBar = (SeekBar) findViewById(R.id.settings_action_interval_seekBar);
		// privacyStatementChB=(CheckBox)findViewById(R.id.settings_privcy_statement_checkBox);
		SOSRepitionTextView = (TextView) findViewById(R.id.textView2);
		ActionIntervalTextView = (TextView) findViewById(R.id.textView3);

		activationChB.setChecked(ServiceManager.getBoolSetting(this,
				ServiceManager.KEY_PANIC));
		// locationAutoReplaySp.setSelection(settings.getInt(ServiceManager.KEY_LOCATION_AUTO_REPLAY,
		// 1));
		nameEditText.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_NAME));
		SOSRepetitionSeekBar.setProgress(ServiceManager.getIntSetting(this,
				ServiceManager.KEY_SOS_REPETITION));
		ActionIntervalSeekBar.setProgress(ServiceManager.getIntSetting(this,
				ServiceManager.KEY_ACTION_INTERVAL));
		// privacyStatementChB.setChecked(settings.getBoolean(ServiceManager.KEY_PRIVACY_STATEMENT,
		// false));

		// ActionIntervalSeekBar.setKeepScreenOn( );
		this.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		int SOSRepetition = SOSRepetitionSeekBar.getProgress();
		if (SOSRepetition == 0)
			SOSRepetition = 1;
		String SOSlabel = "SOS Repetition: '" + SOSRepetition + "'";
		SOSRepitionTextView.setText(SOSlabel);

		int actionInterval = ActionIntervalSeekBar.getProgress();
		if (actionInterval == 0)
			actionInterval = 1;
		String intervallabel = "Action Interval: '" + actionInterval
				+ "' minutes";
		ActionIntervalTextView.setText(intervallabel);

		SOSRepetitionSeekBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						// TODO Auto-generated method stub

						int SOSRepetition = SOSRepetitionSeekBar.getProgress();
						if (SOSRepetition == 0) {
							SOSRepetition = 1;
							SOSRepetitionSeekBar.setProgress(SOSRepetition);
						}

						String SOSlabel = "SOS Repetition: '" + SOSRepetition
								+ "'";
						SOSRepitionTextView.setText(SOSlabel);
						commitChanges();
					}
				});

		ActionIntervalSeekBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						// TODO Auto-generated method stub
						int actionInterval = ActionIntervalSeekBar
								.getProgress();

						if (actionInterval < 5)
							actionInterval = 5;
						else if (actionInterval > 5 && actionInterval < 10)
							actionInterval = 10;
						else if (actionInterval > 10 && actionInterval < 15)
							actionInterval = 15;
						else if (actionInterval > 15 && actionInterval < 20)
							actionInterval = 20;
						else if (actionInterval > 20 && actionInterval < 25)
							actionInterval = 25;
						else if (actionInterval > 25)
							actionInterval = 30;

						ActionIntervalSeekBar.setProgress(actionInterval);
						String intervallabel = "Action Interval: '"
								+ actionInterval + "' minutes";
						ActionIntervalTextView.setText(intervallabel);
						commitChanges();
					}
				});

		nameEditText.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if (arg0 == nameEditText
						&& arg1.getAction() == MotionEvent.ACTION_UP) {
					if (nameEditText.getText().toString().equals("Name"))
						nameEditText.setText("");
				}
				return false;
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		/*
		 * if(v==panicContactsBtn){ startActivity(new
		 * Intent(SettingsActivity.this,PanicSettingsActivity.class)); }
		 */
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */

	@Override
	protected void onDestroy() {

		commitChanges();
		super.onDestroy();
	}

	public void commitChanges() {
		ServiceManager.setBoolSetting(this, ServiceManager.KEY_PANIC,
				activationChB.isChecked());
		// editor.putInt(ServiceManager.KEY_LOCATION_AUTO_REPLAY,
		// locationAutoReplaySp.getSelectedItemPosition());

		ServiceManager.setStringSetting(this, ServiceManager.KEY_NAME,
				nameEditText.getText().toString());

		int SOSRepetition = SOSRepetitionSeekBar.getProgress();
		if (SOSRepetition == 0)
			SOSRepetition = 1;
		ServiceManager.setIntSetting(this, ServiceManager.KEY_SOS_REPETITION,
				SOSRepetition);

		int actionInterval = ActionIntervalSeekBar.getProgress();
		if (actionInterval == 0)
			actionInterval = 1;
		ServiceManager.setIntSetting(this, ServiceManager.KEY_ACTION_INTERVAL,
				actionInterval);

		// editor.putBoolean(ServiceManager.KEY_PRIVACY_STATEMENT,
		// privacyStatementChB.isChecked());

	}
}
